package healthyPointsManager.component
{
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;

	public class PaginationArrayCollection extends ArrayCollection
	{
		private const ITEMS_PER_PAGE:int = 50;
		private const MINIMUM_PAGE:int = 1;
		
		[Bindable]
		private var _totalNumOfPages:int;
		private var _itemsPerPage:int;
		private var _maxIndex:int;
		private var _minIndex:int;
		[Bindable]
		private var _currentPage:int;
	
		
		public function PaginationArrayCollection(source:Array=null, numOfItemsPerPage:int=ITEMS_PER_PAGE)
		{
			super();
			this.source = source;
			itemsPerPage = itemsPerPage;										
		}
		

			
		/*This function is called when the number of items per page has been changed.  This will reset the view and adjust the total number of pages
		as well*/		
		private function setPages():void{
			if(source.length % itemsPerPage == 0){
				_totalNumOfPages = source.length / itemsPerPage;
			}else{
				_totalNumOfPages = (source.length / itemsPerPage) + 1;
			}
			_minIndex = 0;
			_maxIndex = itemsPerPage - 1;
			currentPage = 1;
			updateView();
		}
		
		/*responsible for updating the view by setting the new max and min index, what information is filtered and refreshes the screen.*/
		private function updateView():void{
			_maxIndex = (currentPage * itemsPerPage)-1;
			_minIndex = _maxIndex - (itemsPerPage - 1);
			if(sort != null){
				
			}			
			filterFunction = filterView;
			refresh();			 
		}
		
		//filters out each object whose index isn't within the current min and max index range
		private function filterView(item:Object):Boolean{
			return (getItemIndex(item) >= _minIndex && getItemIndex(item) <= _maxIndex);
		}

		//**********Start of public functions
		
		public function get minIndex():int{
			return _minIndex + 1;
		}
		
		public function get maxIndex():int{
			return _maxIndex + 1;
		}
		
		//total number of pages			
		public function get totalNumOfPages():int{
			return _totalNumOfPages;
		}
		
		//currentPage		
		public function set currentPage(value:int):void{
			if(value < MINIMUM_PAGE){//minor error checking
				value = MINIMUM_PAGE;
			}else if(value > totalNumOfPages){
				value = totalNumOfPages;
			}
			_currentPage = value;
			updateView();
		}		
		[Bindable]
		public function get currentPage():int{
			return _currentPage;
		}
		
		//itemsPerPage
		[Bindable]
		public function get itemsPerPage():int{
			return _itemsPerPage;
		}		
		/*if an invalid value is passed in the the items per page is set to the default value.  Anytime this function is called it automatically
		calls setPages() to reset the total number of pages.*/		
		public function set itemsPerPage(value:int):void{
			if(value <= 0){
				_itemsPerPage = ITEMS_PER_PAGE;
			}else{
				_itemsPerPage = value;
			}
			setPages();
		}
		
		//pages the view up a page if not on the last page
		public function pageUp():void{
			if(currentPage < totalNumOfPages){
				currentPage++;	
			}			
		}
		
		//pages the view down if not one the first page
		public function pageDown():void{
			if(currentPage > MINIMUM_PAGE){
				currentPage--;
			}
		}
		
		
		
	}
}